package com.voltbarn.vbsensors;

import com.voltbarn.vbsensors.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class VbEdit extends Activity {

	private EditText mStationText;
	private RadioButton bVbsp;
	private RadioButton bWu;
	private RadioGroup srg;
    private Long mRowId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);

        mStationText = (EditText) findViewById(R.id.title);
        bVbsp = (RadioButton)findViewById(R.id.vbsp);
        bWu = (RadioButton)findViewById(R.id.wu);
        srg = (RadioGroup)findViewById(R.id.stationRG);

        Button confirmButton = (Button) findViewById(R.id.confirm);

        mRowId = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	// We're modifying an existing entry, so we have info
            String title = extras.getString(VbDBAdapter.KEY_SENSORS);
            mRowId = extras.getLong(VbDBAdapter.KEY_ROWID);
            if (title != null) {
            	mStationText.setText(title);
            	String stype = extras.getString(VbDBAdapter.KEY_STYPE);
            	if (stype.equals("wu")) {
            		bWu.setChecked(true);
            	}
            }
        }
        
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Bundle bundle = new Bundle();

                bundle.putString(VbDBAdapter.KEY_SENSORS, mStationText.getText().toString());
                if (srg.getCheckedRadioButtonId() == R.id.vbsp)
                	bundle.putString(VbDBAdapter.KEY_STYPE, "vbsp");
                else
                	bundle.putString(VbDBAdapter.KEY_STYPE, "wu");
                if (mRowId != null) {
                    bundle.putLong(VbDBAdapter.KEY_ROWID, mRowId);
                }

                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }

        });
    }
}
